//
//  StatusCell.swift
//  SinaWeibo
//
//  Created by 张洪毓 on 15/11/14.
//  Copyright © 2015年 张洪毓. All rights reserved.
//

import UIKit
import SnapKit
let statusCellMargin: CGFloat = 12
let statusCellIconWidth: CGFloat = 35

class StatusCell: UITableViewCell {

    var statusBottomConstrains: Constraint?
    
    //数据模型
    var status: Status? {
        didSet{
            //设置原创微博的数据模型
            originalView.status = status
            //设置转发微博的数据模型
            if let pictures = status?.retweeted_status {
                retweeterView.hidden = false
                retweeterView.retweeterStatus = pictures
                self.statusBottomConstrains?.uninstall()
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.statusBottomConstrains = make.top.equalTo(retweeterView.snp_bottom).constraint
                })
            } else {
                retweeterView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.statusBottomConstrains = make.top.equalTo(originalView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //设置微博选中样式
        selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        //添加子视图
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweeterView)

        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //这里固定死行高的话，那么自动计算行高就会出错
            //make.height.equalTo(50)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
        
        retweeterView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            self.statusBottomConstrains = make.top.equalTo(retweeterView.snp_bottom).constraint
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(40)
        }
    }
    
    //懒加载原创微博视图 StatusOriginalView
    private lazy var originalView: StatusOriginalView = StatusOriginalView()
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    private lazy var retweeterView: StatusRetweeterView = StatusRetweeterView()
}
